package com.haidechizi.fastadmin.modules.sys.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.haidechizi.fastadmin.common.annotation.DataFilter;
import com.haidechizi.fastadmin.common.page.PageDto;
import com.haidechizi.fastadmin.common.utils.PageInfo;
import com.haidechizi.fastadmin.modules.sys.dao.SysUserDao;
import com.haidechizi.fastadmin.modules.sys.entity.SysOffice;
import com.haidechizi.fastadmin.modules.sys.entity.SysUser;
import com.haidechizi.fastadmin.modules.sys.service.SysOfficeService;
import com.haidechizi.fastadmin.modules.sys.service.SysUserRoleService;
import com.haidechizi.fastadmin.modules.sys.service.SysUserService;
import com.haidechizi.fastadmin.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * 系统用户
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysOfficeService sysOfficeService;

    @Override
    public List<Long> queryAllMenuId(Long userId) {
        return baseMapper.queryAllMenuId(userId);
    }

    @Override
    @DataFilter(subOffice = true, user = false)
    public PageInfo queryPage(PageDto pageDto, SysUser sysUser) {
        String username = sysUser.getUsername();

        Wrapper<SysUser> wrapper;
        Page<SysUser> page = this.selectPage(
                pageDto.cover2Page(),
                wrapper = new EntityWrapper<SysUser>()
                        .like(StringUtils.isNotBlank(username), "username", username)
                        .addFilterIfNeed(pageDto.getSqlFilter() != null, pageDto.getSqlFilter())
        );

        for (SysUser user : page.getRecords()) {
            SysOffice sysOffice = sysOfficeService.selectById(user.getOfficeId());
            user.setOfficeName(sysOffice.getName());
        }
        int count = super.selectCount(wrapper
                .addFilterIfNeed(pageDto.getSqlFilter() != null, pageDto.getSqlFilter()));
        page.setTotal(count);

        return new PageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysUser user) {
        user.setCreateTime(new Date());
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        user.setSalt(salt);
        user.setPassword(ShiroUtils.sha256(user.getPassword(), user.getSalt()));
        this.insert(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysUser user) {
        if (StringUtils.isBlank(user.getPassword())) {
            user.setPassword(null);
        } else {
            user.setPassword(ShiroUtils.sha256(user.getPassword(), user.getSalt()));
        }
        this.updateById(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }


    @Override
    public boolean updatePassword(Long userId, String password, String newPassword) {
        SysUser userEntity = new SysUser();
        userEntity.setPassword(newPassword);
        return this.update(userEntity,
                new EntityWrapper<SysUser>().eq("user_id", userId).eq("password", password));
    }

}
